<?php

class ContactController extends Zend_Controller_Action {

    public function init() {
        $this->_helper->layout->setLayout('index');
        $this->view->PageText = '!Lorem ipsum dolor sit amet, consectetur adipisicing elit.'; //Текст в заголовку сторінки
        $this->view->PageTitle = 'Our services'; // Текст заголовок сторінки
        $this->view->PageTitleText = 'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore.'; // Текст підзаголовок
        $this->view->AdressBox = 'Visit us';
        $this->view->PhonesBox = 'Call us';
        $this->view->URLSBox = 'Email us';
    }

    public function indexAction() {
        $GoogleLink = new Default_Model_Table_Contact();
        $this->view->GoogleLink = $GoogleLink->getGoogleLink();
        $this->view->Adressess = $GoogleLink->getAdress();
        $this->view->Phones = $GoogleLink->getPhone();
        $this->view->Links = $GoogleLink->getUrls();
        $MessageForm = new Default_Model_Form_Message();
        $MessageForm->submit->setLabel('Send');
        $this->view->form = $MessageForm;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();

        //------------CAPCHA
        $request = $this->getRequest();
        $captcha = $request->getPost('captcha');
        $captchaId = $captcha['id'];
        $captchaInput = $captcha['input'];
        $captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captchaId);
        $captchaIterator = $captchaSession->getIterator();
        //Zend_Debug::dump($captchaIterator);exit();
        if (isset ($captchaIterator['word'])){$captchaWord = $captchaIterator['word'];}else{$captchaWord='';}
//        if ($captchaInput == $captchaWord) {
//            echo 'ok';exit();
//        } else {
//            echo 'nok';exit();
//        }
        //-----------END CAPCHA===========

            if (($MessageForm->isValid($formData))&&($captchaInput == $captchaWord)) {
                $name = $MessageForm->getValue('name');
                $phone = $MessageForm->getValue('phone');
                $mail = $MessageForm->getValue('mail');
                $message = $MessageForm->getValue('message');
                $addMessage = new Default_Model_Table_Message();
                $addMessage->addMessage($name, $phone, $mail, $message);

                $this->_helper->redirector('index');
            } else {
                $MessageForm->populate($formData);
            }
        }
    }

}

